package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author: Liu
 * @date: 2022/02/26
 * @time: 14:58
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    /**
     * 添加套餐分类
     *
     * @param category
     * @return
     */
    @Override
    public R<String> add(Category category) {

        if (ObjectUtils.isEmpty(category)) {
            return R.error("添加失败！");
        }

        this.saveOrUpdate(category);

        return R.success("添加成功！");
    }

    /**
     * 套餐分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<Page<Category>> page(Long page, Long pageSize) {
        if (ObjectUtils.isEmpty(page)
                || page <= 0) {
            page = 1L;
        }
        if (ObjectUtils.isEmpty(pageSize)
                || pageSize <= 0) {
            page = 10L;
        }

        Page<Category> page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        Page<Category> page2 = this.page(page1, wrapper);

        return R.success(page2);
    }


    /**
     * 删除套餐分类
     *
     * @param id
     * @return
     */
    @Override
    public R<String> delete(Long id) {
        if (ObjectUtils.isEmpty(id) ||
                id <= 0) {
            return R.error("删除失败！");
        }
        Integer integer = dishService.selectByCategoryId(id);
        Integer integer1 = setmealService.selectByCategoryId(id);
        System.out.println(integer + "+++++++++++++++++++++++++++++++++" + integer1);

        if (integer > 0 || integer1 > 0) {
            return R.error("有关联，删除失败！");
        }


        this.removeById(id);
        return R.success("删除成功！");
    }

    /**
     * 修改套餐分类
     *
     * @param category
     * @return
     */
    @Override
    public R<String> update(Category category) {
        if (ObjectUtils.isEmpty(category)
                || ObjectUtils.isEmpty(category.getId())) {
            return R.error("参数异常");
        }
        boolean updateById = this.updateById(category);
        if (!updateById) {
            return R.error("修改分类失败！");
        }
        return R.success("修改分类成功！");
    }

    /**
     * 套餐分类列表
     *
     * @param type
     * @return
     */
    @Override
    public R<List<Category>> list(Integer type) {
    /*    if (ObjectUtils.isEmpty(type)) {
            return R.error("参数错误！");
        }*/
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(type), Category::getType, type);
        List<Category> list = this.list(wrapper);
        return R.success(list);
    }
}
